﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

/// <summary>
///AjaxFileUpload
///执行上传后返回json格式的data
/// </summary>
public class AjaxFileUpload:FileUpload
{    
	public AjaxFileUpload()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    private string btnTxt = "上传";
    public string ButtonText 
    {
        get {
            return this.btnTxt;
        }
        set {
            this.btnTxt = value;
        }
    }

    private string onerr = @"function (data, status, e)
                            {
                                alert(e);
                            }";
    public string OnErrScript 
    {
        get { return this.onerr; }
        set { this.onerr = value; }
    }

    public bool StaticScript { get; set; }

    private string onsucc = @"function (data, status)
                            {
                                if (typeof (data.error) != 'undefined')
                                {
                                    if (data.error != '')
                                    {
                                        alert(data.error);
                                    } else
                                    {
                                        alert(data.msg);
                                    }
                                }
                            }";
    public string OnSuccessScript 
    {
        get { return this.onsucc; }
        set { onsucc = value; }
    }

    public string AllowExt { get; set; }

    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        base.Render(writer);

        string html = "<input value=\""+btnTxt+"\" type='button' onclick=\"return ajaxFileUpload{selfflag}('"+this.ID+"');\" />";
        string script = @"function ajaxFileUpload{selfflag}(UploadCtrlID)
        {
            var ext=$('#'+UploadCtrlID).val();
            var ext=ext.split('.');
            ext=ext[ext.length-1];
            allowexts='{allow}'.split(',');
            var pass=false;
            for (var i=0;i<allowexts.length;i++)
            {
                if (allowexts[i]==ext)
                { pass=true; }
            }
            if (!pass) { alert ('请上传{allow}格式的文件');return; }
            $.ajaxFileUpload
		    ({
		        url: 'doupload.aspx',
		        secureuri: false,
		        fileElementId: UploadCtrlID,
		        dataType: 'json',
		        success: {OnSuccess},
		        error: {OnError}
		    });

			return false;

        }";
        script = script.Replace("{OnError}", OnErrScript).Replace("{OnSuccess}", OnSuccessScript).Replace("{allow}",this.AllowExt);
        string scriptkey = "ajaxUploadScript";
        if (!StaticScript)
        {
            script=script.Replace("{selfflag}", "_" + this.ID);
            scriptkey += "_" + this.ID;
            html=html.Replace("{selfflag}", "_" + this.ID);
        }
        else
        {
            script=script.Replace("{selfflag}", string.Empty);
            html=html.Replace("{selfflag}", string.Empty);
        }

        Helper.WriteScriptAtTop(script, scriptkey);
        writer.Write(html);
    }
}
